package com.ruijie.acl.web;

import java.security.acl.Acl;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.faces.model.SelectItem;

import org.jboss.seam.ScopeType;
import org.jboss.seam.annotations.In;
import org.jboss.seam.annotations.Name;
import org.jboss.seam.annotations.Scope;

import com.ruijie.acl.AccessGroup;
import com.ruijie.acl.AclDevice;
import com.ruijie.acl.AclDeviceAccessGroup;
import com.ruijie.acl.AclInterface;
import com.ruijie.acl.AclInterfaceView;
import com.ruijie.acl.AclPlanInterface;
import com.ruijie.acl.service.AccessGroupService;
import com.ruijie.acl.service.AceService;
import com.ruijie.acl.service.AclInterfaceService;
import com.ruijie.acl.service.DeviceAccessGroupService;
import com.ruijie.acl.task.config.TaskConfigAclDevice;
import com.ruijie.acl.task.model.AccessGroupInfo;
import com.ruijie.acl.task.model.AclInterfaceInfo;
import com.ruijie.acl.utils.AclConfigUtil;
import com.ruijie.acl.utils.AclUtil;
import com.ruijie.acl.utils.InterfaceSortUtil;
import com.ruijie.batch.plan.PlanParameters;
import com.ruijie.emp.device.Device;
import com.ruijie.emp.device.DeviceInterface;
import com.ruijie.foundation.adapter.connection.snmp.util.StringUtil;
import com.ruijie.foundation.adapter.utils.StringUtils;
import com.ruijie.foundation.bo.EntityObject;
import com.ruijie.foundation.exception.BusinessException;
import com.ruijie.foundation.service.Repository;


/**
 * <p>
 * Title: ACL部署计划管理
 * </p>
 * <p>
 * Description: Function Description
 * </p>
 * <p>
 * Copyright: Copyright (c) 2009
 * </p>
 * <p>
 * Company: Ruijie Co., Ltd.
 * </p>
 * <p>
 * Create Time: 2009-09-21
 * </p>
 * 
 * @author ASAI
 *         <p>
 *         Update Time:
 *         </p>
 *         <p>
 *         Updater:
 *         </p>
 *         <p>
 *         Update Comments:
 *         </p>
 */
@Name("deviceSelectInterfacetAction")
@Scope(ScopeType.CONVERSATION)
public class DeviceSelectInterfacetAction extends AclBaseAction {

	
	@In("#{aclInterfaceService}")
	private AclInterfaceService aclInterfaceService;
	
	@In("#{taskConfigAclDevice}")
	private TaskConfigAclDevice taskConfigAclDevice;
	
	@In("#{aceService}")
	private AceService aceService;
	
	@In("#{deviceAccessGroupService}")
	private DeviceAccessGroupService deviceAccessGroupService;
	
	@In("#{accessGroupService}")
	private AccessGroupService accessGroupService;

	private Device device;
	
	private List<AclDeviceAccessGroup> aclDeviceGroupList; 

	private List<AclInterfaceView> aclInterfaceView;

	private String selectedInterfaceByAccessGroup;
	
	private String initInterfaceByAccessGroup;//用于存储初始的接口值

	private Map<Long, List<AclInterface>> interfaceMap = new HashMap<Long, List<AclInterface>>();
	
	private List<AclInterface> aclInterfaceList = new ArrayList<AclInterface>();
	
	@In("#{aclDeviceAction}")
	private AclDeviceAction aclDeviceAction;
	
	private AclDevice aclDevice;

	/**
	 * 取得设备的所有接口
	 * 
	 * @param device
	 *            设备
	 * @return 接口列表
	 */
	public List<AclInterfaceView> getAclInterfaceView(Device device) {
		if (aclInterfaceView == null || this.device == null) {
			this.device = device;
			aclInterfaceView = aclInterfaceService
					.getAclInterfaceViewByDevice(device);
			aclInterfaceView = InterfaceSortUtil
			.sortInterfaceItemList(aclInterfaceView);
		}
		return aclInterfaceView;
	}

	/**
	 * 取得设备的所有接口
	 * 
	 * @param device
	 *            设备
	 * @return 接口列表
	 */
	public List<AclInterfaceView> getAclInterfaceView() {
		if (this.device != null) {
			aclInterfaceView = aclInterfaceService
					.getAclInterfaceViewByDevice(this.device);
			aclInterfaceView = InterfaceSortUtil
					.sortInterfaceItemList(aclInterfaceView);
		}
		return aclInterfaceView;
	}

	/**
	 * 取得设备的所有接口
	 * 
	 * @param device
	 *            设备
	 * @return 接口列表
	 */
	public List<SelectItem> getAclInterface() {
		if (this.device != null) {
			this.aclInterfaceView = null;
			return InterfaceSortUtil
					.sortInterfaceItemListSelect(getAclInterfaceView(this.device));
		}
		return new ArrayList<SelectItem>();
	}
	
	
	/**
	 * 部署ACL组到接口
	 * @param id
	 * @param device
	 */
	public void reConfig(Device device,AclDevice aclDevice){
		try {		
			aclInterfaceService.reConfigSelectInterface(device, 
					convertString2Aclgroup(selectedInterfaceByAccessGroup),
					selectedInterfaceByAccessGroup);	
			List<AclInterface> interfaces = getAclInterfaces(selectedInterfaceByAccessGroup);
			aclInterfaceService.removeByDevice(device);//删除acl接口
			saveAclInterface(interfaces,selectedInterfaceByAccessGroup);
			aclDeviceAction.setModel(aclDevice);
			redirectTo("/acl/aclDeviceDetail.seam");
		} catch (BusinessException e) {
			selectedInterfaceByAccessGroup=initInterfaceByAccessGroup;
			throw e;
		}
		
	}
	
	/**
	 * 把字符串转换成acl组
	 * @param planInterfaceString 
	 * @return
	 */
	public List<AccessGroup> convertString2Aclgroup(String planInterfaceString) {
		List<AccessGroup> accessGoupList = null;
		if (!AclUtil.empty(planInterfaceString)) {
			accessGoupList = new ArrayList<AccessGroup>();
			String[] arrAclInterfaceList = planInterfaceString.split(";");
			for (String interfaceStr : arrAclInterfaceList) {
				AccessGroup accessGroupInfo = null;
				String[] arrAclInterfaces = interfaceStr.split(",");
				if (arrAclInterfaces.length == 3) {
					if(!arrAclInterfaces[2].equals("-1")){
						accessGroupInfo = accessGroupService.get(Long.valueOf(arrAclInterfaces[2]));
					}
				}
				if(accessGroupInfo!=null){
					accessGoupList.add(accessGroupInfo);
				}
			}
		}
		Set<AccessGroup> set = new HashSet<AccessGroup>(accessGoupList);
		accessGoupList.clear();
		accessGoupList.addAll(set);
		return accessGoupList;
	}

	
	public String convertAclPlanInterface2String(Device device) {
		return convertAclPlanInterface2String(device.getId());
	}
	
	public String convertAclPlanInterface2String(Long id) {
		if (this.interfaceMap.containsKey(id)) {
			return convertAclPlanInterface2String(this.interfaceMap.get(id));
		}
		return "";
	}
	
	public String convertAclPlanInterface2String(
			List<AclInterface> aclInterfaceList) {
		String resultString = "";
		if (aclInterfaceList != null && aclInterfaceList.size() > 0) {
			for (AclInterface aclInterface : aclInterfaceList) {
				resultString += aclInterface.getDeviceInterface().getId()
						+ ",";
				if (aclInterface.getInAccessGroup() != null && aclInterface.getOutAccessGroup() != null) {
					resultString += "InOut," + aclInterface.getInAccessGroup().getId() + ";";
				}else if (aclInterface.getInAccessGroup() != null) {
					resultString += "In," + aclInterface.getInAccessGroup().getId() + ";";
				}else if (aclInterface.getOutAccessGroup() != null) {
					resultString += "Out," + aclInterface.getOutAccessGroup().getId() + ";";
				}else{
					if (!StringUtils.isNullOrEmpty(aclInterface.getInAccessGroupName())&& 
							!StringUtils.isNullOrEmpty(aclInterface.getOutAccessGroupName())){
						resultString += "InOut,-1;";
					}else if (!StringUtils.isNullOrEmpty(aclInterface.getInAccessGroupName())) {
						resultString += "In,-1;";
					}else if (!StringUtils.isNullOrEmpty(aclInterface.getOutAccessGroupName())) {
						resultString += "Out,-1;";
					}
				}
			}
		}
		return resultString;
	}
	
	public List<AclInterface> getAclInterfaces(String planInterfaceString){
		List<AclInterface> interfaces = new ArrayList<AclInterface>();
		if (!AclUtil.empty(planInterfaceString)) {		
			String[] arrAclInterfaceList = planInterfaceString.split(";");
			for (String interfaceStr : arrAclInterfaceList) {
				String[] arrAclInterfaces = interfaceStr.split(",");
				DeviceInterface deviceInterface=null;
				if (arrAclInterfaces.length == 3) {
					AclInterface interfaceInfo = new AclInterface();
					interfaceInfo.setDevice(device);
					deviceInterface = aclInterfaceService.getDeviceInterface(Long.valueOf(arrAclInterfaces[0]));
					interfaceInfo.setDeviceInterface(deviceInterface);
					if(arrAclInterfaces[2].equals("-1")){
						AclInterface interfaceNoGroup = aclInterfaceService.getAclInterfaceByDeviceInterface(Long.valueOf(arrAclInterfaces[0]));
						if("InOut".equals(arrAclInterfaces[1])){
							interfaceInfo.setInAccessGroupName(interfaceNoGroup.getInAccessGroupName());
							interfaceInfo.setOutAccessGroupName(interfaceNoGroup.getOutAccessGroupName());
						}else if("In".equals(arrAclInterfaces[1])){
							interfaceInfo.setInAccessGroupName(interfaceNoGroup.getInAccessGroupName());
						}else{
							interfaceInfo.setOutAccessGroupName(interfaceNoGroup.getOutAccessGroupName());
						}
					}else{
						AccessGroup accessGroup = accessGroupService.get(Long.valueOf(arrAclInterfaces[2]));
						if("InOut".equals(arrAclInterfaces[1])){
							interfaceInfo.setInAccessGroup(accessGroup);
							interfaceInfo.setOutAccessGroup(accessGroup);
							interfaceInfo.setInAccessGroupName(accessGroup.getName());
							interfaceInfo.setOutAccessGroupName(accessGroup.getName());
						}else if("In".equals(arrAclInterfaces[1])){
							interfaceInfo.setInAccessGroup(accessGroup);
							interfaceInfo.setInAccessGroupName(accessGroup.getName());
						}else{
							interfaceInfo.setOutAccessGroup(accessGroup);
							interfaceInfo.setOutAccessGroupName(accessGroup.getName());
						}
					}
					interfaces.add(interfaceInfo);
				}
			}
		}
		return interfaces;
	}
	
	/**
	 * 把字符串转换成接口类型保存
	 * @param planInterfaceString 
	 * @return
	 */
	public void saveAclInterface(List<AclInterface> aclInterfaces,String planInterfaceString) {
		if (!AclUtil.empty(planInterfaceString)) {		
			for(AclInterface interfaceInfo:aclInterfaces){
				AclInterface aclInterface = aclInterfaceService.getByDeviceAndInterface(device, interfaceInfo.getDeviceInterface());
				/*判断接口是否已经存在，存在就更新*/
				if(aclInterface!=null){
					if(!StringUtils.isNullOrEmpty(interfaceInfo.getInAccessGroupName())){
						aclInterface.setInAccessGroup(interfaceInfo.getInAccessGroup());
						aclInterface.setInAccessGroupName(interfaceInfo.getInAccessGroupName());
					}
					if(!StringUtils.isNullOrEmpty(interfaceInfo.getOutAccessGroupName())){
						aclInterface.setOutAccessGroup(interfaceInfo.getOutAccessGroup());
						aclInterface.setOutAccessGroupName(interfaceInfo.getOutAccessGroupName());
					}
					aclInterfaceService.update(aclInterface);
				}else{
					aclInterfaceService.save(interfaceInfo);
				}
			}
		}
	}
	
	public List<AclInterface> getAclInterface(Device device){
		if (device != null){
			return this.getInterfaceMap().get(device.getId());
		}else{
			return new ArrayList<AclInterface>();
		}
	}

	public Device getDevice() {
		return device;
	}

	public void setDevice(Device device) {
		this.device = device;
	}

	public String getSelectedInterfaceByAccessGroup() {
		return selectedInterfaceByAccessGroup;
	}

	public void setSelectedInterfaceByAccessGroup(
			String selectedInterfaceByAccessGroup) {
		this.selectedInterfaceByAccessGroup = selectedInterfaceByAccessGroup;
	}	
	
	@SuppressWarnings("unchecked")
	@Override
	protected Repository getRepository() {
		return this.aclInterfaceService;
	}
	
	public String getSelectPage(Device device,AclDevice aclDevice){
		this.device = device;
		this.aclDevice = aclDevice;
		this.aclDeviceGroupList = deviceAccessGroupService.getByAclDevice(aclDevice);
		List<AclInterface> aclInterfaceList = AclConfigUtil.converAclInterfaceList(aclInterfaceService.getListByDevice(device));
		interfaceMap.put(device.getId(), aclInterfaceList);
		if (this.interfaceMap.containsKey(device.getId())) {
			this.aclInterfaceList = this.interfaceMap.get(device.getId());
			this.selectedInterfaceByAccessGroup = convertAclPlanInterface2String(this.aclInterfaceList);
			this.initInterfaceByAccessGroup = selectedInterfaceByAccessGroup;
		}else{
			this.selectedInterfaceByAccessGroup = "";
			this.initInterfaceByAccessGroup = "";
		}
		return "/acl/deviceSelectInterface.seam";
	}
	
	

	@Override
	protected String getAddPage() {
		return "/acl/timeRangeAdd.seam";
	}

	@Override
	public EntityObject getCriteria() {
		return null;
	}

	@Override
	protected String getDetailPage() {
		return "/acl/timeRangeDetail.seam";
	}

	@Override
	protected String getListPage() {
		return "/acl/timeRangeIndex.seam";
	}

	@Override
	public EntityObject getModel() {
		return null;
	}

	@Override
	protected String getUpdatePage() {
		return "/acl/timeRangeEdit.seam";
	}

	@Override
	public void resetModel() {
		
	}
	
	@Override
	public void setCriteria(EntityObject criteria) {
		
	}

	@Override
	public void setModel(EntityObject model) {
		
	}

	public List<AclDeviceAccessGroup> getAclDeviceGroupList() {
		return aclDeviceGroupList;
	}

	public void setAclDeviceGroupList(List<AclDeviceAccessGroup> aclDeviceGroupList) {
		this.aclDeviceGroupList = aclDeviceGroupList;
	}

	public AclDevice getAclDevice() {
		return aclDevice;
	}

	public void setAclDevice(AclDevice aclDevice) {
		this.aclDevice = aclDevice;
	}

	public Map<Long, List<AclInterface>> getInterfaceMap() {
		return interfaceMap;
	}

	public void setInterfaceMap(Map<Long, List<AclInterface>> interfaceMap) {
		this.interfaceMap = interfaceMap;
	}

	public List<AclInterface> getAclInterfaceList() {
		return aclInterfaceList;
	}

	public void setAclInterfaceList(List<AclInterface> aclInterfaceList) {
		this.aclInterfaceList = aclInterfaceList;
	}

	public String getInitInterfaceByAccessGroup() {
		return initInterfaceByAccessGroup;
	}

	public void setInitInterfaceByAccessGroup(String initInterfaceByAccessGroup) {
		this.initInterfaceByAccessGroup = initInterfaceByAccessGroup;
	}

}
